package com.cskaoyan.servlet;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.service.SystemService;
import com.cskaoyan.service.impl.SystemServiceImpl;
import com.cskaoyan.utils.JacksonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/config/*")
public class AdminConfigServlet extends CommonServlet {

    SystemService systemService = new SystemServiceImpl();

    public BaseRespVo mall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String methodName = request.getMethod();
        if ("GET".equals(methodName)) {
            return queryMall(request, response);
        } else
            return updateMall(request, response);
    }

    private BaseRespVo updateMall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");

        String jsonStr = request.getReader().readLine();
        Map<String, String> map = JacksonUtil.read(jsonStr, Map.class);
        systemService.updateMall(map);
        return BaseRespVo.ok(null);
    }

    private BaseRespVo queryMall(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> dataMap = systemService.queryMall();
        return BaseRespVo.ok(dataMap);
    }

    public BaseRespVo wx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String methodName = request.getMethod();
        if ("GET".equals(methodName)) {
            return queryWx(request, response);
        } else return updateWx(request, response);
    }

    private BaseRespVo updateWx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String jsonStr = request.getReader().readLine();
        Map<String,Object> map = JacksonUtil.read(jsonStr, Map.class);
        systemService.updateWx(map);
        return BaseRespVo.ok(null);
    }

    private BaseRespVo queryWx(HttpServletRequest request, HttpServletResponse response) {
        Map<String,String> dataMap = systemService.queryWx();
        return BaseRespVo.ok(dataMap);
    }

    public BaseRespVo express(HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            HashMap hashMap = systemService.getExpressConfig();
            return BaseRespVo.ok(hashMap);
        } else {
            String jsonStr = request.getReader().readLine();
            HashMap hashMap = JacksonUtil.read(jsonStr, HashMap.class);
            systemService.postExpressConfig(hashMap);
            return BaseRespVo.ok(null);
        }
    }

    public BaseRespVo order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            HashMap<String, String> hashMap = systemService.getOrderConfig();
            return BaseRespVo.ok(hashMap);
        } else {
            String jsonStr = request.getReader().readLine();
            HashMap hashMap = JacksonUtil.read(jsonStr, HashMap.class);
            systemService.postOrderConfig(hashMap);
            return BaseRespVo.fail("成功", 0);
        }
    }
}

